/*
 * 整体妆容的分类管理
 * @Author: zfx
 * @Date: 2018-06-26 17:52:07
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
    .v-customer-classify-views {
        padding: 20px;
	}
	@deep: ~'>>>';
	@{deep} .ivu-modal-body{
		min-height: 160px;
	}
</style>

<template>
	<div class="v-customer-classify-views">
		<Tabs
			v-model="tag_name"
			type="card"
			@on-click="changeTab">

			<TabPane
				:label="$t('common.create')"
				name="新增">

				<classify-create
					ref="create"
					:loading="button_loading"
					@post="postData"/>

			</TabPane>

			<TabPane
				:label="$t('common.check')"
				name="查看">

				<classify-index
					:loading="table_loading"
					:classify-data="classify.data"
					@edit="classifyDetail"
					@delete="deleteClassify"
					@sort="confirmSearch"/>
				<br>
				<!-- 分页组件 -->
				<Row
					type="flex"
					justify="center">
					<Page
						:total="classify.total"
						:current="classify.current_page"
						:page-size="query_info.per_page"
						placement="top"
						show-sizer
						show-total
						@on-change="pageChange"
						@on-page-size-change="perPageChange"/>
				</Row>

			</TabPane>
		</Tabs>

		<Modal
			v-model="show_modal"
			:title="$t('classification.update_classify')"
			class-name="vertical-middle-modal">

			<!-- 整体妆容的分类编辑组件 -->
			<classify-edit
				v-if="show_modal"
				ref="edit"
				:classify-detail="classify_details"
				:loading="button_loading"
				@update="updateClassify"/>

			<div slot="footer"/>
		</Modal>

	</div>

</template>

<script>

import qs from 'qs';
import { mapMutations } from 'vuex';
import API_LIST from 'src/API_LIST';
import classifyCreate from './create';
import classifyIndex from './index';
import classifyEdit from './edit';

export default {

	name: 'ClassificationViews',

	components: {
		classifyCreate,

		classifyIndex,

		classifyEdit,
	},
	data() {
		return {

			query_info: {
				page: 1,
				per_page: 20,
			}, // 页码

			search_info: {
				page: 1,
				per_page: 20,

			}, // 分页信息

			tag_name: '新增',

			table_loading: false, // 表格更新时的loading

			show_modal: false, // 弹框的控制

			classify_details: {}, // 分类列表详情

			classify: {}, // 分类列表

			button_loading: false, // 按钮的loading
		};
	},

	watch: {
		show_modal() {
			if (!this.show_modal) {
				this.classify_details = null;
				this.$refs.edit.handleReset('formValidate');
			}
		},
	},
	methods: {

		...mapMutations('spinShow', [
			'showSpin',
			'closeSpin',
		]),

		/**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined}
         */
		changeTab(tab_name) {
			if (tab_name === '查看') {
				this.fetchClassifyList(this.query_info);

				this.$refs.create.handleReset('formValidate');
			} else {
				this.classify.data = [];
			}
		},

		/**
         * 页码改变
         * @param  {string} pageNumber {当前页码}
         * @return {undefined}
         */
		pageChange(pageNumber) {
			this.search_info.page = pageNumber;

			this.search_info.per_page = this.query_info.per_page;

			this.fetchClassifyList(this.search_info);
		},

		/**
         * 每页显示数改变
         * @param  {string} per_page {当前每页显示数}
         * @return {undefined}
         */
		perPageChange(per_page) {
			this.search_info.per_page = per_page;

			this.query_info.per_page = per_page;

			this.fetchClassifyList(this.query_info);
		},

		/**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
		confirmSearch({
			keyword, sort_column, sort_by,
		}) {
			const param = {
				page: this.query_info.page,

				per_page: this.query_info.per_page,

				name: keyword,
			};

			switch (sort_by) {
			case '':

				break;
			default:
				param.order_bys = [
					{
						sort: sort_by,
						order_by: sort_column,
					},
				];
			}

			this.search_info = JSON.parse(JSON.stringify(param));

			this.fetchClassifyList(this.search_info);
		},

		/**
         * 新增分类的post数据
         * @return {undefined}
         */
		postData(param) {
			this.showSpin(this.$t('common.please_wait'));

			this.$http.post(API_LIST.CUST_CLASSIFY, param)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.create_success'));

					this.$refs.create.handleReset('formValidate');
				})
				.catch((err) => {
					this.$Message.error(err.message);
				})
				.then(this.closeSpin);
		},

		/**
         * 查看分类列表详情
         * @param  {string} id {商品详情id}
         * @return {undefined}
         */
		classifyDetail(detail) {
			this.classify_details = detail;

			this.show_modal = true;
		},

		/**
         * 更新分类列表信息
         * @param  {Object} param {需要更新的参数对象}
         * @return {undefined}
         */
		updateClassify(id, param) {
			this.showSpin();

			this.$http.post(`${API_LIST.CUST_CLASSIFY}/${id}`, param)

				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.update_success'));

					this.show_modal = false;

					this.fetchClassifyList(this.search_info);
				})

				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})

				.then(this.closeSpin);
		},

		/**
         * 删除分类
         * @param  {string} id {需要删除的分类的id}
         * @return {undefined}
         */
		deleteClassify(id) {
			this.$Modal.confirm({

				title: this.$t('product.tip'), // '温馨提示',
				content: this.$t('product.is_delete'), // '删除之后将无法恢复，确定吗？',

				onOk: () => {
					const param = {

						_method: 'DELETE',

					};
					this.table_loading = true;

					this.$http.post(`${API_LIST.CUST_CLASSIFY}/${id}`, param)

						.then(({ data }) => {
							if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

							this.fetchClassifyList(this.search_info);

							this.$Message.success(this.$t('common.delete_success'));
						})

						.catch((err) => {
							this.$Message.error(err.message || err.statusText);
						})

						.then(() => { this.table_loading = false; });
				},
				onCancel: () => {},

			});
		},

		/**
         * 获取分类列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
		fetchClassifyList(param) {
			this.table_loading = true;

			const query_str = qs.stringify(param);

			this.$http.get(`${API_LIST.CUST_CLASSIFY}?${query_str}`)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);
					this.classify = data.pack_categories;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},

	},
};
</script>
